package top.riske.gen3d.controller;


import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import top.riske.gen3d.DTO.HunyuanInputMultiViewImagesDto;
import top.riske.gen3d.DTO.HunyuanInputParamDto;
import top.riske.gen3d.common.DTO.JwtDto;
import top.riske.gen3d.http.exception.BisException;
import top.riske.gen3d.http.request.SubmitJobForTextRequest;
import top.riske.gen3d.http.response.Hunyuan3DJobResponse;
import top.riske.gen3d.http.response.Result;
import top.riske.gen3d.entity.Hunyuan3DJob;
import top.riske.gen3d.service.Hunyuan3DService;

import java.util.List;


@RestController
@RequestMapping("/api")
public class Hunyuan3DController {

    @Resource
    private Hunyuan3DService hunyuan3DService;

    /**
     * 提交任务, 描述文本生成3D模型
     * @param inputParam
     * @return
     */
    @PostMapping("/protected/user/hunyuan/submit/text")
    public Result<Hunyuan3DJob> submitJobForText(@RequestBody HunyuanInputParamDto inputParam, JwtDto jwtDto) {
        Hunyuan3DJob job = null;
        try {
            job = hunyuan3DService.submitJobForText(inputParam, jwtDto.getUid());
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
            throw new BisException("提交任务失败,请稍后再试:"+ e.getMessage());
        }
        return Result.success(job);
    }

    /**
     * 提交任务, 图片生成3D模型
     * @param inputParam 图片信息
     * @param jwtDto 用户信息
     * @return 任务结果
     */
    @PostMapping("/protected/user/hunyuan/submit/image")
    public Result<Hunyuan3DJob> submitJobForImage(@RequestBody HunyuanInputParamDto inputParam, JwtDto jwtDto) {
        Hunyuan3DJob job = null;
        try {
            job = hunyuan3DService.submitJobForImage(inputParam, jwtDto.getUid());
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
            throw new BisException("提交任务失败,请稍后再试:"+ e.getMessage());
        }
        return Result.success(job);
    }


    /**
     * 查询任务
     * @param id 任务主键ID
     * @return 任务结果
     */
    @GetMapping("/protected/user/hunyuan/query")
    public Result<Hunyuan3DJob> queryJob(@RequestParam Long id, JwtDto jwtDto, HttpServletRequest request) {
        Hunyuan3DJob response;
        try {
            response = hunyuan3DService.queryJob(id, jwtDto.getUid(), request);
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
            throw new BisException("查询任务失败,请稍后再试:"+e.getMessage());
        }
        return Result.success(response);
    }

    /**
     * 重新提交任务
     * @param id 任务主键ID
     * @return
     */
    @PostMapping("/protected/user/hunyuan/submit/retry")
    public Result<Hunyuan3DJob> submitJobForRetry(@RequestParam Long id, JwtDto jwtDto) {
        Hunyuan3DJob job = null;
        try {
            job = hunyuan3DService.submitJobForRetry(id, jwtDto.getUid());
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
            throw new BisException("提交任务失败,请稍后再试:"+ e.getMessage());
        }
        return Result.success(job);
    }


}
